// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using SnapObjects.Commons;

namespace SnapObjects.Data
{
    internal class DynamicPropertyMeta : IDynamicProperty, IPropertyInfo, IBindableProperty
    {
        public DynamicPropertyMeta(string name, DbColumnInfo column)
        {
            this.Name = name;
            this.Index = column.ColumnOrdinal;
            this.IsNotInWhere = column.IsNotInWhere;
            this.TypeName = column.TypeName;
            this.IsNullable = column.IsNullable;
            this.MappedColumn = column.ColumnName;
            this.IsKey = column.IsPrimaryKey;
            this.IsIdentity = column.IsIdentity;
            this.MappedColumn = column.ColumnName;
            this.IsTimestamp = column.IsTimestamp;
            this.SaveStrategy = SaveStrategy.Ignore;
            
            this.DataType = column.IsNullable ?
                DynamicModelHelp.GetPropertyType(column.DataType) : column.DataType;
        }

        public DynamicPropertyMeta(int index, string name, Type type)
        {
            this.Name = name;
            this.DataType = type;
            this.Index = index;
            this.IsNullable = type.IsNullable();
            this.MappedColumn = this.Name;
            this.SaveStrategy = SaveStrategy.Ignore;
        }

        public int Index { get; }

        public string Name { get; }

        public Type DataType { get; }

        public bool IsNullable { get; }

        public bool IsBindable => true;

        public string MappedColumn { get; protected set; }

        public bool IsKey { get; protected set; }

        public bool IsIdentity { get; protected set; }

        public bool IsConcurrencyCheck { get; protected set; }

        public SaveStrategy SaveStrategy { get; protected set; }

        public bool IsNotInWhere { get; }

        public bool IsTimestamp { get; protected set; }

        public bool HasConversion { get; set; }

        public ValueConverter ValueConverter { get; set; }

        public string TypeName { get; }
    }
}
